package com.hwua.view;

import com.hwua.domain.Car;
import com.hwua.service.CarService;
import com.hwua.serviceimpl.CarServiceImpl;
import com.hwua.util.InputTools;
import com.hwua.util.PrintTransformTools;

import java.util.List;

/**
 * 按照汽车类型查找界面
 * @author admin
 */
public class FindCarByTypeView extends AbstractView{
    private int num2;
    private String name;
    private String pwd;
    public FindCarByTypeView(int num2,String name,String pwd){
        this.num2=num2;
        this.name=name;
        this.pwd=pwd;
    }
    @Override
    public AbstractView showView() {
        String sign = "类型id";
        String rentStatus=null;
        CarService cs = new CarServiceImpl();
        List<Car> carList3 = cs.findCarById(num2,sign);
        System.out.println("===============================================指定汽车类型的汽车=======================================");
        System.out.println("编号\t汽车名称\t\t备注\t\t\t品牌\t\t类型\t\t\t颜色\t\t日租金\t\t是否可租");
        for (Car car : carList3) {
            if(car.getRentStatus()==0){
                rentStatus="可租";
            }else{
                rentStatus="不可租";
            }
            String brandStr = PrintTransformTools.BrandTransForm(car.getBrand());
            String typeStr = PrintTransformTools.TypeTransForm(car.getType());
            if(car.getPutAwayStatus()==0){
                System.out.println(car.getCarId()+"\t\t"+car.getCarName()+"\t\t\t"+car.getCarRemark()+"\t\t"+
                        brandStr+"\t\t"+typeStr+"\t\t"+car.getColor()+"\t\t"+car.getDailyRent()+"\t\t"+rentStatus);
            }
        }
        System.out.println("===============================================================================================");
        System.out.println();
        System.out.println("输入0：           退出");
        System.out.println("输入1+汽车编号：  进入租车订单  如1+2");
        System.out.println("输入2+1：         按价格降序    2+2： 按价格升序");
        System.out.println("输入3+类型编号：  按类型搜索");
        System.out.println("输入4+品牌编号：  按品牌搜索");
        System.out.println("输入5：           查看全部汽车");
        System.out.println("输入6：           查看我的租车记录");
        System.out.println("输入7+汽车编号：  还车");
        while (true){
            String choose = InputTools.nextString();
            //使用正则表达式匹配录入的字符串是否符合“1+1”或者“1” 格式
            boolean matches = choose.matches("[0-9]{1}(\\+\\d+)?");
            if(matches){
                //判断键盘录入的字符串长度是否大于1，如果大于1，则截取
                if(choose.length()>1){
                    //通过String类的split方法分割字符串
                    String[] strings = choose.split("\\+");
                    //  获得键盘录入字符串中 + 号前面的字符并转换成int类型
                    int num1=Integer.parseInt(String.valueOf(strings[0]));
                    //  键盘录入字符串中 + 号后面的字符并转换成int类型
                    int num2=Integer.parseInt(String.valueOf(strings[1]));
                    switch (num1){
                        case 1:
                            nextView=new RentCarView(num2,name,pwd);
                            break;
                        case 2:
                            nextView=new UserSortByPriceView(num2,name,pwd);
                            break;
                        case 3:
                            nextView=new FindCarByTypeView(num2,name,pwd);
                            break;
                        case 4:
                            nextView=new FindCarByBrandView(num2,name,pwd);
                            break;
                        case 7:
                            nextView= new ReturnCarView(num2,name,pwd,"汽车查询界面");
                            break;
                        default:
                            System.out.println("超过可选范围，请重新输入：");
                            continue;
                    }
                }else{
                    switch (choose){
                        case "5":
                            nextView=new UserView(name,pwd);
                            break;
                        case "6":
                            nextView=new UserLookRecordView(name,pwd);
                            break;
                        case "0":
                            nextView=null;
                            break;
                        default:
                            System.out.println("超过可选范围，请重新输入：");
                            continue;
                    }
                }
                break;
            }else {
                System.out.println("输入有误，请重新输入：");
            }
        }
        return nextView;
    }
}
